home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Data 2002 May / CD Rom Data Mayıs 2002.iso / Freeware / Blitz Basic / data1.cab / Support / help / samples / insectoids.bb < prev    next >
Encoding:
Text File  |  2002-04-10  |  12.0 KB  |  642 lines

  1.  
  2. Const width=640,height=480,num_rots=64
  3.  
  4. Dim alien_rots(num_rots)
  5.  
  6. Global game_state,game_timer,level_name$,alien_speed
  7. Global num_aliens,num_flying,fly_timer,num_bulls,num_players
  8. Global c_x#,c_y#,c_xs#,c_ys#,c_dir#,c_phase#,rev_dir,c_speed#,c_xsize#,c_ysize#
  9. Global player_image,stars_image,bomb_image,bull_image,stars_scroll,boom_anim
  10. Global mini_ship,tmp_image,insectoids_image
  11.  
  12. Global boom_sound=LoadSound( "sounds\boom.wav" )
  13. Global cool_sound=LoadSound( "sounds\cool.wav" )
  14. Global kazap_sound=LoadSound( "sounds\kazap.wav" )
  15. Global shoot_sound=LoadSound( "sounds\shoot.wav" )
  16.  
  17. Type Alien
  18.     Field x#,y#,rot,state
  19.     Field f_x#,f_y#
  20.     Field dest_y#,dest_rot,rot_step,bomb_cnt
  21. End Type
  22.  
  23. Type Player
  24.     Field x,y,state,lives,ctrl,bang,score
  25. End Type
  26.  
  27. Type Bomb
  28.     Field x#,y#,xs#,ys#
  29. End Type
  30.  
  31. Type Bullet
  32.     Field x,y
  33. End Type
  34.  
  35. Type Explosion
  36.     Field x,y,frame
  37. End Type
  38.  
  39. Graphics width,height
  40. SetBuffer BackBuffer()
  41.  
  42. SetFont LoadFont( "helvetica",16,True )
  43.  
  44. tmp_image=CreateImage( width,FontHeight() )
  45.  
  46. LoadGraphics()
  47.  
  48. game_state=0
  49. game_timer=0
  50.  
  51. time=MilliSecs()
  52.  
  53. While Not KeyHit(1)
  54.  
  55.     n_time=MilliSecs()
  56.     elapsed=n_time-time
  57.     If elapsed>25        ;slowing down! - clamp update to 40 FPS (1000/40=25 millisecs)
  58.         cnt=elapsed/25
  59.         For k=1 To cnt
  60.             UpdateGame()
  61.         Next
  62.         time=time+cnt*25
  63.     Else                ;leave frame synced...
  64.         UpdateGame()
  65.         time=n_time
  66.     EndIf
  67.     
  68.     RenderGame()
  69.     
  70.     Flip
  71. Wend
  72.  
  73. End
  74.  
  75. Function CreatePlayer( ctrl )
  76.     p.Player=New Player
  77.     p\lives=3
  78.     p\ctrl=ctrl
  79.     ResetPlayer( p )
  80.     num_players=num_players+1
  81. End Function
  82.  
  83. Function ResetPlayer( p.Player )
  84.     p\x=width/2
  85.     p\y=height-20
  86.     p\state=1
  87. End Function
  88.  
  89. Function UpdatePlayer( p.Player )
  90.  
  91.     If p\state<>1 Then Return
  92.  
  93.     Local l,r,f
  94.     Select p\ctrl
  95.     Case 1
  96.         l=KeyDown( 203 )
  97.         r=KeyDown( 205 )
  98.         f=KeyHit( 57 )
  99.     Case 2
  100.         jx=JoyXDir()
  101.         If jx<0 Then l=1 Else If jx>0 Then r=1
  102.         f=JoyHit(1)
  103.     End Select
  104.     If l
  105.         If p\x>16 Then p\x=p\x-4
  106.     Else If r
  107.         If p\x<width-16 Then p\x=p\x+4
  108.     EndIf
  109.     
  110.     If game_state<>2 Then Return
  111.     
  112.     If f And num_bulls<3
  113.         PlaySound shoot_sound
  114.         b.Bullet=New Bullet
  115.         b\x=p\x:b\y=p\y-16
  116.         num_bulls=num_bulls+1
  117.     EndIf
  118.  
  119.     dead=False    
  120.     For a.Alien=Each Alien
  121.         If ImagesOverlap( player_image,p\x,p\y,alien_rots( a\rot*num_rots/360 ),a\x,a\y )
  122.             dead=True
  123.             Exit
  124.         EndIf
  125.     Next
  126.     For bb.Bomb=Each Bomb
  127.         If ImagesOverlap( player_image,p\x,p\y,bomb_image,bb\x,bb\y )
  128.             dead=True
  129.             Exit
  130.         EndIf
  131.     Next
  132.     
  133.     If Not dead Then Return
  134.     
  135.     PlaySound boom_sound
  136.     
  137.     For k=-105 To 0 Step 15
  138.         CreateExplosion( p\x+Rnd(-10,10),p\y+Rnd(-10,10),k )
  139.     Next
  140.     
  141.     p\bang=1
  142.     p\state=2
  143.     p\lives=p\lives-1
  144.     game_state=3
  145.     
  146. End Function
  147.  
  148. Function RenderPlayer( p.Player )
  149.  
  150.     Color 255,255,255
  151.     Text width/2,4,p\score,1
  152.  
  153.     For k=1 To p\lives
  154.         DrawImage mini_ship,k*16+8,14
  155.     Next
  156.     
  157.     If p\state=1
  158.         DrawImage player_image,p\x,p\y
  159.         Return
  160.     EndIf
  161.     
  162.     p\bang=p\bang+8
  163.     
  164.     r#=p\bang
  165.     For i=255 To 1 Step -15
  166.         Color i,i,i
  167.         For an=0 To 359 Step 6
  168.             x=p\x+Cos(an)*r
  169.             y=p\y+Sin(an)*r
  170.             Rect x,y,3,3
  171.         Next
  172.         r=r-6:If r<=0 Then Exit
  173.     Next
  174.     
  175. End Function
  176.  
  177. Function AddPoints( p.Player,pnts )
  178.     t=p\score/5000
  179.     p\score=p\score+pnts
  180.     If p\score/5000<>t Then p\lives=p\lives+1
  181. End Function
  182.  
  183. Function UpdateBullet( b.Bullet )
  184.     b\y=b\y-5
  185.         
  186.     For a.Alien=Each Alien
  187.         If ImagesOverlap( bull_image,b\x,b\y,alien_rots( a\rot*num_rots/360 ),a\x,a\y )
  188.             PlaySound kazap_sound
  189.             If a\state=1
  190.                 pnts=25
  191.             Else If a\state=2
  192.                 pnts=50
  193.             Else
  194.                 pnts=100
  195.             EndIf
  196.             AddPoints( First Player,pnts )
  197.             CreateExplosion( a\x,a\y )
  198.             Delete b:num_bulls=num_bulls-1
  199.             If a\state<>1 num_flying=num_flying-1
  200.             num_aliens=num_aliens-1
  201.             Delete a
  202.             Return
  203.         EndIf
  204.     Next
  205.     
  206.     If b\y>0 Then Return
  207.     Delete b:num_bulls=num_bulls-1
  208.     
  209. End Function
  210.  
  211. Function RenderBullet( b.Bullet )
  212.     DrawImage bull_image,b\x,b\y
  213. End Function
  214.  
  215. Function UpdateBomb( b.Bomb )
  216.     b\x=b\x+b\xs
  217.     b\y=b\y+b\ys
  218.     If b\y>height Then Delete b
  219. End Function
  220.  
  221. Function RenderBomb( b.Bomb )
  222.     DrawImage bomb_image,b\x,b\y
  223. End Function
  224.  
  225. Function UpdateAlien( a.Alien )
  226.  
  227.     Select a\state
  228.     Case 1
  229.         If a\rot<>0
  230.             If a\rot>180 a\rot=a\rot+6 Else a\rot=a\rot-6
  231.             If a\rot<0 Or a\rot>=360 Then a\rot=0
  232.         EndIf
  233.         dx=c_x+a\f_x*c_xs - a\x
  234.         dy=c_y+a\f_y*c_ys - a\y
  235.         If dx<-alien_speed Then dx=-alien_speed Else If dx>alien_speed Then dx=alien_speed
  236.         If dy<-alien_speed Then dy=-alien_speed Else If dy>alien_speed Then dy=alien_speed
  237.         a\x=a\x+dx:a\y=a\y+dy
  238.         If c_dir<0 And a\x<16 Then rev_dir=True
  239.         If c_dir>0 And a\x>width-16 Then rev_dir=True
  240.     Case 2
  241.         a\rot=a\rot+a\rot_step
  242.         If a\rot<0 Then a\rot=a\rot+360 Else If a\rot>=360 Then a\rot=a\rot-360
  243.         If a\rot<90 Or a\rot>270
  244.             a\dest_rot=Rnd(180-40,180+40)
  245.             a\dest_y=a\dest_y+Rnd( 100,300 )
  246.             a\state=3
  247.         EndIf
  248.         a\x=a\x+Cos( a\rot-90 )*alien_speed
  249.         a\y=a\y+Sin( a\rot-90 )*alien_speed
  250.         DropBomb( a )
  251.     Case 3        
  252.         dr=a\rot-a\dest_rot
  253.         If Abs(dr)>Abs(a\rot_step)
  254.             a\rot=a\rot+a\rot_step
  255.             If a\rot<0 Then a\rot=a\rot+360 Else If a\rot>=360 Then a\rot=a\rot-360
  256.         EndIf
  257.         a\x=a\x+Cos( a\rot-90 )*alien_speed
  258.         a\y=a\y+Sin( a\rot-90 )*alien_speed
  259.         If a\y>height
  260.             a\x=Rnd(width/2)+width/4:a\y=0
  261.             num_flying=num_flying-1
  262.             a\state=1
  263.         Else If a\y>a\dest_y
  264.             a\rot_step=-a\rot_step
  265.             a\state=2
  266.         EndIf
  267.         DropBomb( a )
  268.     End Select
  269. End Function
  270.  
  271. Function RenderAlien( a.Alien )
  272.     DrawImage alien_rots( a\rot*num_rots/360 ),a\x,a\y
  273. End Function
  274.  
  275. Function UpdateExplosion( e.Explosion )
  276.     e\frame=e\frame+1
  277.     If e\frame=18 Then Delete e
  278. End Function
  279.  
  280. Function RenderExplosion( e.Explosion )
  281.     If e\frame<0 Then Return
  282.     DrawImage boom_anim,e\x,e\y,e\frame/3
  283. End Function
  284.  
  285. Function CreateExplosion( x,y,frame=0 )
  286.     e.Explosion=New Explosion
  287.     e\x=x:e\y=y:e\frame=frame
  288. End Function
  289.  
  290. Function DropBomb( a.Alien )
  291.     If a\bomb_cnt=0 Then a\bomb_cnt=Rnd(50,100)
  292.     a\bomb_cnt=a\bomb_cnt-1
  293.     If a\bomb_cnt>0 Then Return
  294.     p.Player=First Player
  295.     If p=Null Then Return
  296.     b.Bomb=New Bomb
  297.     b\x=a\x
  298.     b\y=a\y
  299.     If a\x<p\x Then b\xs=1 Else b\xs=-1
  300.     b\ys=4
  301. End Function
  302.  
  303. Function UpdateFormation()
  304.  
  305.     c_phase=(c_phase+c_speed)Mod 360
  306.     t#=Sin( c_phase )*.5+.5
  307.     c_xs=t*c_xsize+1:c_ys=t*c_ysize+1
  308.     
  309.     If game_state<>1 Then c_x=c_x+c_dir
  310.     
  311. End Function
  312.  
  313. Function UpdateFlyTimer()
  314.  
  315.     If num_aliens>3
  316.         If fly_timer=0 Then fly_timer=600
  317.         fly_timer=fly_timer-1
  318.         If fly_timer>120 Then Return
  319.         If fly_timer Mod 30<>0 Then Return
  320.     EndIf
  321.     
  322.     n=Rnd( num_aliens-num_flying )
  323.     
  324.     For a.Alien=Each Alien
  325.         If a\state=1
  326.             If n=0
  327.                 a\dest_y=a\y
  328.                 a\rot_step=3:If Rnd(1)<.5 Then a\rot_step=-3
  329.                 num_flying=num_flying+1
  330.                 a\state=2
  331.                 Return
  332.             EndIf
  333.             n=n-1
  334.         EndIf
  335.     Next
  336. End Function
  337.  
  338. Function UpdateGame()
  339.  
  340.     Select game_state
  341.     Case 0
  342.         game_timer=game_timer+1
  343.         If KeyHit(57) Then BeginGame()
  344.     Case 1
  345.         game_timer=game_timer+1
  346.         If game_timer=150 Then game_state=2
  347.         UpdateFormation()
  348.     Case 2
  349.         UpdateFlyTimer()
  350.         UpdateFormation()
  351.         If num_aliens=0 Then BeginLevel()
  352.     Case 3
  353.         UpdateFormation()
  354.         If num_flying=0 And First Explosion=Null
  355.             p.Player=First Player
  356.             If p\lives>0
  357.                 ResetPlayer( p )
  358.                 game_state=2
  359.                 FlushKeys()
  360.             Else
  361.                 game_state=4
  362.                 game_timer=0
  363.             EndIf
  364.         EndIf
  365.     Case 4
  366.         UpdateFlyTimer()
  367.         UpdateFormation()
  368.         game_timer=game_timer+1
  369.         If game_timer=150 Then EndGame()
  370.     End Select
  371.     
  372.     rev_dir=False
  373.     For a.Alien=Each Alien
  374.         UpdateAlien( a )
  375.     Next
  376.     If rev_dir Then c_dir=-c_dir
  377.     
  378.     For bb.Bomb=Each Bomb
  379.         UpdateBomb( bb )
  380.     Next
  381.     
  382.     For p.Player=Each Player
  383.         UpdatePlayer( p )
  384.     Next
  385.     
  386.     For b.Bullet=Each Bullet
  387.         UpdateBullet( b )
  388.     Next
  389.     
  390.     For e.Explosion=Each Explosion
  391.         UpdateExplosion( e )
  392.     Next
  393. End Function
  394.  
  395. Function RenderGame()
  396.  
  397.     TileBlock stars_image,0,stars_scroll
  398.     TileImage stars_image,7,stars_scroll*2
  399.     TileImage stars_image,23,stars_scroll*3
  400.     stars_scroll=(stars_scroll+1) Mod ImageHeight( stars_image )
  401.     
  402.     For a.Alien=Each Alien
  403.         RenderAlien( a )
  404.     Next
  405.     
  406.     For bb.Bomb=Each Bomb
  407.         RenderBomb( bb )
  408.     Next
  409.     
  410.     For p.Player=Each Player
  411.         RenderPlayer( p )
  412.     Next
  413.     
  414.     For b.Bullet=Each Bullet
  415.         RenderBullet( b )
  416.     Next
  417.     
  418.     For e.Explosion=Each Explosion
  419.         RenderExplosion( e )
  420.     Next
  421.     
  422.     Select game_state
  423.     Case 0
  424.         DrawImage insectoids_image,width/2,height/3
  425.         If game_timer<150 Or (game_timer-150) Mod 80<40
  426.             Color 255,255,255:TitleText( "PRESS SPACE TO START",width/2,height-FontHeight()*2,game_timer )
  427.         EndIf
  428.     Case 1
  429.         Rainbow( game_timer*5 )
  430.         TitleText( level_name$,width/2,height/2,game_timer*2 )
  431.     Case 4
  432.         Color 255,255,255
  433.         Text width/2,height/2,"GAME OVER",1,1
  434.     End Select
  435.     
  436. End Function
  437.  
  438. Function TitleText( txt$,x,y,time )
  439.     n=0
  440.     If time<100 Then n=100-time
  441.     If n=1
  442.         Text x,y,txt$,1,1
  443.     Else
  444.         ExplodeText( x,y,txt$,n*.5+1,n+1 )
  445.     EndIf
  446. End Function
  447.  
  448. Function LoadGraphics()
  449.  
  450.     AutoMidHandle True
  451.     
  452.     stars_image=LoadImage( "graphics\stars.bmp" )
  453.     player_image=LoadImage( "graphics\player.bmp" );:ScaleImage player_image,.75,.75
  454.     mini_ship=CopyImage( player_image ):ScaleImage mini_ship,.4,.4
  455.     bull_image=LoadImage( "graphics\bullet.bmp" ):ScaleImage bull_image,.75,1
  456.     bomb_image=LoadImage( "graphics\bbomb.bmp" ):ScaleImage bomb_image,.5,.5
  457.     boom_anim=LoadAnimImage( "graphics\kaboom.bmp",60,48,0,6 )
  458.     insectoids_image=LoadImage( "graphics\insectoids_logo.bmp" )
  459.     ResizeImage boom_anim,32,32:ResizeImage boom_anim,56,56
  460.     i=LoadImage( "graphics\alien.bmp" ):ScaleImage i,.65,.65
  461.     For k=0 To num_rots-1
  462.         alien_rots(k)=CopyImage( i )
  463.         RotateImage alien_rots(k),k*360/num_rots
  464.     Next
  465. End Function
  466.  
  467. Function BeginLevel()
  468.  
  469.     PlaySound cool_sound
  470.     
  471.     Read level_name$
  472.     If level_name$=""
  473.         Restore .levels
  474.         Read level_name$
  475.         alien_speed=alien_speed+1
  476.         If alien_speed>6 Then alien_speed=6
  477.     EndIf
  478.     
  479.     c_x=width/2:c_y=104:c_phase=0:c_dir=1
  480.     
  481.     Read c_speed,c_xsize,c_ysize
  482.     
  483.     Repeat
  484.         Read x
  485.         If x=999 Then Exit
  486.         Read y,cnt
  487.         For k=1 To cnt
  488.             a.Alien=New Alien
  489.             a\x=c_x
  490.             a\y=c_y
  491.             a\rot=0
  492.             a\state=1
  493.             a\f_x=x*34
  494.             a\f_y=y*24
  495.             x=x+1
  496.         Next
  497.         num_aliens=num_aliens+cnt
  498.     Forever
  499.  
  500.     game_state=1
  501.     game_timer=0
  502.     
  503. End Function
  504.  
  505. Function BeginGame()
  506.     
  507.     level=0
  508.     num_bulls=0
  509.     num_aliens=0
  510.     num_flying=0
  511.     game_state=0
  512.     num_players=0
  513.     alien_speed=3
  514.     
  515.     CreatePlayer( 1 )
  516.     
  517.     Restore .levels
  518.     
  519.     BeginLevel()
  520.     
  521.     FlushKeys()
  522.     
  523. End Function
  524.  
  525. Function EndGame()
  526.  
  527.     Delete Each Player
  528.     Delete Each Bullet
  529.     Delete Each Alien
  530.     Delete Each Bomb
  531.  
  532.     game_state=0
  533.     game_timer=0
  534.  
  535.     FlushKeys()
  536.  
  537. End Function
  538.  
  539. Function Rainbow( time )
  540.     r=time Mod 768:If r>255 Then r=511-r
  541.     g=(time+256)Mod 768:If g>255 Then g=511-g
  542.     b=(time+512) Mod 768:If b>255 Then b=511-b
  543.     If r<0 Then r=0
  544.     If g<0 Then g=0
  545.     If b<0 Then b=0
  546.     Color r,g,b
  547. End Function
  548.  
  549. Function FlushKeys()
  550.     While GetKey():Wend
  551.     For k=1 To 255:KeyHit(k):Next
  552. End Function
  553.  
  554. Function ExplodeText( x,y,txt$,xn#,yn# )
  555.     w=StringWidth( txt$ )
  556.     h=FontHeight()
  557.     SetBuffer ImageBuffer( tmp_image )
  558.     Cls:Text 0,0,txt$
  559.     SetBuffer BackBuffer()
  560.     For ty=0 To h-1 Step 4
  561.         For tx=0 To w-1 Step 4
  562.             xo#=(tx-w/2)*xn
  563.             yo#=(ty-h/2)*yn
  564.             DrawImageRect tmp_image,x+xo,y+yo,tx,ty,4,4
  565.         Next
  566.     Next
  567. End Function
  568.  
  569. .levels
  570.  
  571. Data "LEVEL 1"
  572. Data 1,.25,0
  573. Data -2,-2,5
  574. Data -3,-1,7
  575. Data -3,0,7
  576. Data -3,1,7
  577. Data -3,2,7
  578. Data 999
  579.  
  580. Data "LEVEL 2"
  581. Data 1,.25,.25
  582. Data 0,-2,1
  583. Data -1,-1,3
  584. Data -2,0,5
  585. Data -3,1,7
  586. Data -4,2,9
  587. Data -5,3,11
  588. Data 999
  589.  
  590. Data "LEVEL 3"
  591. Data 3,.25,.5
  592. Data -5,-2,11
  593. Data -4,-1,9
  594. Data -3,0,7
  595. Data -4,1,9
  596. Data -5,2,11
  597. Data 999
  598.  
  599. Data "LEVEL 4"
  600. Data 2,0,1
  601. Data -5,-1,11
  602. Data -5,0,11
  603. Data -5,1,11
  604. Data -5,2,11
  605. Data -5,3,11
  606. Data -5,4,11
  607. Data -5,5,11
  608. Data 999
  609.  
  610. Data "LEVEL 5"
  611. Data 1,.25,.125
  612. Data -3,-2,7
  613. Data -4,-1,9
  614. Data -5,0,11
  615. Data -5,1,11
  616. Data -5,2,11
  617. Data -5,3,11
  618. Data -5,4,11
  619. Data -5,5,11
  620. Data -5,6,11
  621. Data -5,7,11
  622. Data 999
  623.  
  624. Data "LEVEL 6"
  625. Data 1,.25,.125
  626. Data -7,-2,15
  627. Data -7,-1,15
  628. Data -7,0,15
  629. Data -7,1,15
  630. Data -7,2,15
  631. Data -7,3,15
  632. Data -7,4,15
  633. Data -7,5,15
  634. Data -7,6,15
  635. Data -7,7,15
  636. Data -7,8,15
  637. Data -7,9,15
  638. Data -7,10,15
  639. Data -7,11,15
  640. Data 999
  641.  
  642. Data ""